import requests
from faker import Faker

fake = Faker("zh_CN")
host = "http://192.168.1.252:8080"
login_url = "/v7/login"
login_data = {"loginname": "guest", "password": "guest"}
login_headers = {"Content-Type": "application/json"}
session = requests.Session()
login_response = session.post(url=host + login_url, json=login_data, headers=login_headers)
login_status = login_response.status_code

print("登录状态码", login_status,"登录结果", login_response.json())
token = login_response.json()["token"]
print("获取token" + token)

# 新增员工
add_employee_url = "/pimpeople"
employee_data = {"pimpersonname": fake.name(), "ygbh": "20240629-01", "zjlx": 13, "zjhm": fake.ssn(), "ygzt": 31}
add_employee_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}
add_employee_response = session.post(url=host + add_employee_url, json=employee_data, headers=add_employee_headers)
add_employee_status = add_employee_response.status_code
print(add_employee_status, "新增员工结果", add_employee_response.json())
# 保存员工信息
# 获取新增员工的id
new_employee_id = add_employee_response.json()["pimpersonid"]
print("新增员工id", new_employee_id)

# 修改员工信息
modify_employee_url = f"/pimpeople/{new_employee_id}"
newName1 = fake.name()
modify_employee_data = {"pimpersonname": newName1, "ygbh": "20240629-02", "zjlx": 13, "zjhm": fake.ssn(), "ygzt": 31}
modify_employee_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}
modify_employee_response = session.put(url=host + modify_employee_url, json=modify_employee_data,
                                       headers=modify_employee_headers)
modify_employee_status = modify_employee_response.status_code
print(modify_employee_status, "修改员工信息结果", modify_employee_response.json())
print("原id", new_employee_id, "新id", modify_employee_response.json()["pimpersonid"])
# 上传头像
upload_avatar_url = f"/ibizutil/upload"
avatar_file = open(r"D:\Python\Project\project01\fakers\fake_data\fake_image.jpg", "rb")
upload_avatar_data = {"file": avatar_file}
upload_response = session.post(url=host + upload_avatar_url, files=upload_avatar_data)
avatar_file.close()
upload_status = upload_response.status_code
print(upload_status, "上传头像结果", upload_response.json())
# 获取上传头像的id
avatar_id = upload_response.json()["fileid"]
avatar_name = upload_response.json()["filename"]
print("上传头像id为", "fileid")
# 下载头像
download_avatar_url = f"/ibizutil/download/{avatar_id}"
download_response = session.get(url=host + download_avatar_url)
download_file = open(f"{avatar_name}", "wb")
download_file.write(download_response.content)
download_file.close()
print("下载头像成功")

# 获取附件的值
file_id = avatar_id
file_name = avatar_name
# 新增教育信息
add_education_url = f"/pimpeople/{new_employee_id}/pimeducations"
add_education_data = {"byyx": "111",
                      "jssj": "2024-06-19",
                      "pimeducationid": None,
                      "qssj": "2024-06-19",
                      "sfdyxl": 0,
                      "sfzgxl": "10",
                      "srfparentdename": "OrmOrg",
                      "srfparentkey": "64BCDCC1-24AA-42E4-AC7A-FB6F242F3FF1",
                      "sxzy": "111",
                      "xkml": "0BD50F57-DD0D-4BFE-99F1-6569F0EBA17C",
                      "xl": "14",
                      "xllx": "04",
                      "xxxz": "30",
                      "fj": r"[{\"name\":\"8525111.jpg\",\"id\":\"d8a514e2e4fca2ceb8a0a1f0b3a13b37\"}]"
                      }
add_education_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}
add_education_response = session.post(url=host + add_education_url, json=add_education_data,
                                      headers=add_education_headers)
adeducation_status = add_education_response.status_code
print(adeducation_status, "新增教育信息结果", add_education_response.json())

# 新增工作履历
add_work_url = f"/pimpeople/{new_employee_id}/pimworkhistories"
add_work_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}
add_work_data = {
    "rzkssj": "2024-05-27",
    "ormpostname": "444",
    "cfplx": "20",
    "pimworkhistoryid": None,
    "ormdutyname": "333",
    "ormorgname": "111",
    "ormorgsectorname": "222",
    "experience": "10",
    "rzjssj": "2024-06-03"
}
add_work_response = session.post(url=host + add_work_url, json=add_work_data, headers=add_work_headers)
add_work_status = add_work_response.status_code
print(add_work_status, "新增工作履历结果", add_work_response.json())

# 新增家庭成员信息
add_family_url = f"/pimpeople/{new_employee_id}/pimfaminfos"
add_family_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}
add_family_data = {
    "pimfaminfoid": None,
    "pimfaminfoname": "阿达",
    "pimpersonname": "沈*明111",
    "ybrgx": "50",
    "zjlx": "10",
    "sruvivors": 0,
    "jlss": "10",
}
add_family_response = session.post(url=host + add_family_url, json=add_family_data, headers=add_family_headers)
add_family_status = add_family_response.status_code
print(add_family_status, "新增家庭成员结果", add_family_response.json())

# 新增奖惩信息
add_punish_url = f"/pimpeople/{new_employee_id}/pimrewardpunishments"
add_punish_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}
add_punish_data = {
    "pimrewardpunishmentid": None,
    "pimrewardpunishmentname": "11",
    "lx": "REWARD",
    "jcjb": "40",
    "jlsj": "2024-06-11",
    "department": "11",
    "jlss": "10",

}
add_punish_response = session.post(url=host + add_punish_url, json=add_punish_data, headers=add_punish_headers)
add_punish_status = add_punish_response.status_code
print(add_punish_status, "新增奖惩信息结果", add_punish_response.json())
# 新增语言能力
add_language_url = f"/pimpeople/{new_employee_id}/pimlanguageabilities"
add_language_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}
add_language_data = {
    "srfparentdename": "OrmOrg",
    "srfparentkey": "64BCDCC1-24AA-42E4-AC7A-FB6F242F3FF1",
    "fj": "[{\"name\":\"8525111.jpg\",\"id\":\"d8a514e2e4fca2ceb8a0a1f0b3a13b37\"}]",
    "wydjhqsj": "2024-06-20",
    "wyyz": "ak",
    "pimlanguageabilityid": None,
    "wydj": "221"
}
add_language_response = session.post(url=host + add_language_url, json=add_language_data, headers=add_language_headers)
add_language_status = add_language_response.status_code
print(add_language_status, "新增语言能力结果", add_language_response.json())
